package com.aj.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.User;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.AddFamilyRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;

public class MarryActivity extends BaseActivity {
	private EditText enter_lovehomenumber;
	private EditText enter_telphone;
	private EditText enter_description;
	private Button sure_button;
	private UserDao userDao;
	private User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_marry);
		setActionBarTitle("结婚");
		initView();
	}

	private void initView() {
		enter_lovehomenumber = (EditText) findViewById(R.id.enter_lovehomenumber);
		enter_telphone = (EditText) findViewById(R.id.enter_telphone);
		enter_description = (EditText) findViewById(R.id.enter_description);
		sure_button = (Button) findViewById(R.id.sure_button);
		userDao = new UserDao(this);
		user = userDao.getCurrentUser(LoveHomeApplication.token);
		sure_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!enter_lovehomenumber.getText().toString().trim()
						.equals("")
						&& enter_telphone.getText().toString().trim()
								.equals("")) {
					Toast.makeText(MarryActivity.this, "请输入爱家号或手机号~",
							Toast.LENGTH_LONG).show();
					return;
				}

				String callId = "5";
				if (user.getSex().equals("1")) {
					callId = "6";
				}
				showProgressDialog();
				new AddFamilyRequest().excute(MarryActivity.this, user
						.getUser_id(), callId, enter_description.getText()
						.toString(), enter_lovehomenumber.getText().toString(),
						"1", enter_telphone.getText().toString(),
						new IHttpCallBack() {

							@Override
							public void success(String result) {
								try {
									dimissProgressDialog();
									Common common = new Common(new JSONObject(
											result));
									if (common
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
										finish();
									}
									Toast.makeText(MarryActivity.this,
											common.getErrorMsg(),
											Toast.LENGTH_LONG).show();
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}

							@Override
							public void fail(String result) {
								try {
									dimissProgressDialog();
									Common common = new Common(new JSONObject(
											result));
									Toast.makeText(MarryActivity.this,
											common.getErrorMsg(),
											Toast.LENGTH_LONG).show();
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}
						});
			}
		});

	}
}
